<?php

/**
 * @category   MM
 * @package    MM_Service
 */
class MM_Service_Admin_EventSchedule extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('MM_Mapper_EventSchedule');
		$this->setModel('MM_Model_EventSchedule');
	}
	
	/**
	 * 更新议程
	 * @param int $esid
	 * @param array $params
	 * @return Mediad2_Model_Result
	 */
	public function updateSchedule($esid, array $params)
	{
		try {
			//获取指定id的event schedule
			$result = $this->find($esid);
			if ($result->getCode()==$result::CODE_FAILURE) return $result;
			$model = $result->getData();
	
			//封装model
			if (isset($params['pesid'])) $model->setPesid($params['pesid']);
			if (isset($params['title'])) $model->setTitle($params['title']);
			if (isset($params['begindate'])) $model->setBegindate($params['begindate']);
			if (isset($params['startdate'])) $model->setStartdate($params['startdate']);
			if (isset($params['enddate'])) $model->setEnddate($params['enddate']);
			if (isset($params['num'])) $model->setNum($params['num']);
			if (isset($params['subtitle'])) $model->setSubtitle($params['subtitle']);
			if (isset($params['desc'])) $model->setDesc($params['desc']);
			if (isset($params['venueid'])) $model->setVenueid($params['venueid']);
			if (isset($params['venuedesc'])) $model->setVenuedesc($params['venuedesc']);
			$gids = @$_REQUEST['gids'];
			if($gids){
				$model->setGids(implode(",", $gids));
			}else{
				$model->setGids(null);
			}
			$result = $this->update($model);
			if($result->getCode()){
				//匹配活动嘉宾信息 by vincent
				$guest = new MM_Service_Admin_EventGuests();
				$esid = $model->getEsid();
				$uid = $model->getUid();
				$eid = $model->getEid();
				//*业务流程,先全部删除再绑定gids 20130731
				
				//删掉活动嘉宾
				$where = sprintf("eid= '%s' and esid = '%s'",$eid,$esid);
				$guest->deleteBy($where);
				
				if(isset($gids)){//绑定活动嘉宾
					foreach ($gids as $key => $value) {
						$guest->chooseGuests($uid, $value, $eid,$esid);
					}
				}
			}	
			return $result;
	
		} catch (Zend_Exception $e) {
			$this->handleException($e); //出错处理
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE); //返回结果--失败
		}
	}
	
}